package com.iman.manjusri.web.restpass;

 
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.Constants;
import com.iman.common.emaiUtil.MailSenderInfo;
import com.iman.common.emaiUtil.SimpleMailSender;
import com.iman.common.util.ConfigUtil;
import com.iman.common.util.DateUtils;
import com.iman.common.util.EncodeUtils;
import com.iman.common.util.EncryptTool;
import com.iman.common.util.Saltcheckutil;
import com.iman.manjusri.repository.IRedisGo;
import com.iman.manjusri.service.UserService;
import com.iman.manjusri.web.BaseController;
/*
 * 
 */

/*
 * *************************************************************************
 * @文件名称: ShopController.java
 *
 * @包路径  : com.iman.manjusri.web.shop 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:  
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2015年3月11日 - 上午12:46:52 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */
@Controller
@RequestMapping(value="/reset/*")
public class RestPassController  extends BaseController {
	
	private  Logger logger = LoggerFactory.getLogger(RestPassController.class);

	@Autowired
	private UserService userService;
	@Inject
    IRedisGo redis;

	 /**
	  * 
	  *  Function:
	  *  功能说明：
	  *	 使用说明：
	  *  @author  wangxiaolei  DateTime 2015年3月11日 上午12:47:30
	  *	返回类型: ModelAndView    
	  *  @param request
	  *  @return
	  */
	@RequestMapping(value="/forget")
	public ModelAndView forget(HttpServletRequest request){
		ModelAndView view =  new ModelAndView("restpass/forget");
		
		putResourceSrc(view);		
		return view;
	}

	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年3月11日 上午12:48:26
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value = "/suremail")
	public ModelAndView suremail(HttpServletRequest request, String loginName, String ranCode) {
		ModelAndView view = null;

		String randCheckCode = (String) request.getSession().getAttribute("randCheckCode");
		Map<String, Object> mapInfo = new HashMap<String, Object>();
		mapInfo.put("loginName", loginName);

		if (StringUtils.equalsIgnoreCase(ranCode, randCheckCode) && userService.getUserCnt(mapInfo) > 0) {
			request.getSession().setAttribute(Constants.REST_PASS_USER, loginName);
			view = new ModelAndView("restpass/suremail");
		} else {
			view = new ModelAndView("restpass/forget");
		}
		putResourceSrc(view);
		return view;
	}
	
	
 
	@RequestMapping(value = "/validatemail")
	public ModelAndView validatemail(HttpServletRequest request, String emailAddr) {
		ModelAndView view = null;

		String loginName = (String) request.getSession().getAttribute(Constants.REST_PASS_USER);
		if (StringUtils.isEmpty(loginName)) {// 跳回输入用户名页面
			view = new ModelAndView("restpass/forget");
		} else {
			Map<String, Object> mapInfo = new HashMap<String, Object>();
			mapInfo.put("loginName", loginName);
			mapInfo.put("emailAddr", emailAddr);
			if (userService.getUserCnt(mapInfo) > 0) {
				String redisKey = Constants.REST_PASS_TOKEN + loginName;
				String token = EncodeUtils.md5(redisKey + System.currentTimeMillis() + (new Random()).nextInt());
				redis.setExiperse(redisKey, token, Constants.SECOND_HALF_HOUR);
				String url = forward_main + "/reset/repassword?loginName=" + loginName + "&token=" + token;

				MailSenderInfo mailInfo = new MailSenderInfo();
				mailInfo.setMailServerHost(ConfigUtil.getString("sendmail.host")/* "smtp.126.com" */);
				mailInfo.setMailServerPort(ConfigUtil.getString("sendmail.port") /* "25" */);
				mailInfo.setValidate(true);
				mailInfo.setUserName(ConfigUtil.getString("sendmail.username") /* "tianshuxiaoleitest@126.com" */);
				mailInfo.setPassword(ConfigUtil.getString("sendmail.password") /* "12qwaszx" */);
				mailInfo.setFromAddress(ConfigUtil.getString("sendmail.fromaddress") /* "tianshuxiaoleitest@126.com" */);
				mailInfo.setToAddress(emailAddr);
				mailInfo.setSubject("德瑞姆心理学院密码重置");
				mailInfo.setContent("请点击下面链接进入修改密码页面：" + url);

				SimpleMailSender.getInstance().sendHtmlMail(mailInfo);

				if (mailInfo.getState() == 200) {
					view = new ModelAndView("restpass/suremailsucc");
					view.addObject("emailAddr", emailAddr.trim().substring(emailAddr.indexOf("@") + 1));
				} else {
					view = new ModelAndView("restpass/suremailfail");
				}
			} else {// 重新输入注册邮箱
				view = new ModelAndView("restpass/suremailfail");
			}
		}
		putResourceSrc(view);
		return view;
	}

	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年3月11日 上午12:47:59
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value = "/repassword")
	public ModelAndView repassword(HttpServletRequest request, String loginName, String token) {
		ModelAndView view = null;

		String token_ = redis.getKV(Constants.REST_PASS_TOKEN + loginName);
		if (StringUtils.equalsIgnoreCase(token, token_)) {
			request.getSession().setAttribute("loginName", loginName);
			request.getSession().setAttribute("token", token);
			view = new ModelAndView("restpass/repassword");
		} else {
			view = new ModelAndView("restpass/forget");
		}

		putResourceSrc(view);
		return view;
	}

	@RequestMapping(value = "/updatepass")
	@ResponseBody
	public Map<String, Integer> updatepass(HttpServletRequest request, String password) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		try {
			String loginName = (String) request.getSession().getAttribute("loginName");
			String token = (String) request.getSession().getAttribute("token");
			String token_ = redis.getKV(Constants.REST_PASS_TOKEN + loginName);

			if (StringUtils.equalsIgnoreCase(token, token_)) {
				String salt = EncryptTool.md5Hex(EncryptTool.sha1HEX(DateUtils.getCurrentStringDateYYMDHMSS()));
				String pass = Saltcheckutil.saltArithmetic(password, salt);
				userService.updatePass(loginName, salt, pass);
				map.put("state", 1);
			}
		} catch (Exception e) {
			logger.error("update pass error! ", e);
		}
		return map;
	}

	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年3月11日 上午12:52:01
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value="/suremailfail")
	public ModelAndView suremailfail(HttpServletRequest request){
		ModelAndView view =  new ModelAndView("restpass/suremailfail");	

		putResourceSrc(view);		
		return view;
	}

	
	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年3月11日 上午12:52:24
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value="/suremailsucc")
	public ModelAndView suremailsucc(HttpServletRequest request){
		ModelAndView view =  new ModelAndView("restpass/suremailsucc");	

		putResourceSrc(view);
		return view;
	}

}
